package com.andnnl.chat;

import com.andnnl.utils.BytesUtils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.Arrays;
import java.util.List;


/**
 * Created by chenss on 2024/3/28.
 */
public class Ucs2Test {

    public static void main(String[] args) {
//        test1();
//        test3();
//        test2();
        test4();
    }

    private static void test4() {
        String message = "你好，世界！";
//        Charset charset = StandardCharsets.UTF_16;
        Charset charset = StandardCharsets.UTF_16BE;
        ByteBuffer encodedMessage = charset.encode(message);
        byte[] messageData = encodedMessage.array();
        System.out.println(messageData.length);
        String s = BytesUtils.bytesToHexStr(messageData);
        System.out.println(s);
        //Long.parseLong(id, 36);
        List<String> ids = Arrays.asList("ah33Dtu8XD", "jbEnoN3Nls", "2t3IUhWu3P", "3YbwBHmUPb","2TlmvCXOqO","cMqGs5Cl3g","AEB4ChubuD","2k5S2XayiK");
//        List<String> ids = Arrays.asList("ah33dtu8xd", "jbenon3nls", "2t3iuhwu3p", "3ybwbhmupb","2tlmvcxoqo","cmqgs5cl3g","aeb4chubud","2k5s2xayik");
//        id:2k5S2XayiK
//        36
//        259995059082812
        for (String id : ids) {
            System.out.println("**************************************");
            System.out.println("id:"+id);
            for (int i = 0; i < 100; i += 2) {
                try {
//                    long idx = Long.parseLong(id, i) & 0xffffffff;
                    long idx = Long.parseLong(id, i);
                    System.out.println(i);
                    System.out.println(idx);
                } catch (NumberFormatException e) {

                }
            }
        }


//        msg:[你好，世界！] => 4F 60 59 7D FF 0C 4E 16 75 4C FF 01"

    }

    private static void test3() {
        System.out.println("test3");
        String original = "[asdf夺工工阿斯顿发asdf 枯塔顶 f222";
//        String original = "【JiaYing】Your verification code is 254573.Please complete the registration within 30 minutes.模压2";

        // 将字符串转换为UCS-2编码的字节数组
        Charset charset = StandardCharsets.UTF_16BE; // 使用UCS-2编码（即UTF-16LE）
        ByteBuffer byteBuffer = charset.encode(original);
        byte[] ucs2Bytes = byteBuffer.array();

        // 将UCS-2编码的字节数组转换回字符串
        CharBuffer charBuffer = charset.decode(ByteBuffer.wrap(ucs2Bytes));
        String result = charBuffer.toString();

        System.out.println("Original string: " + original);
        System.out.println("Converted string: " + result);
    }

    private static void test1() {
//        String original = "【JiaYing】Your verification code is 254573.Please complete the registration within 30 minutes.";
        String original = "【JiaYing】Your verification code is 254573.Please complete the registration within 30 minutes.模压12";

        // 将字符串从UTF-8编码转换为UCS-2编码
        Charset utf8Charset = StandardCharsets.UTF_8;
        Charset ucs2Charset = StandardCharsets.UTF_16LE; // 使用UCS-2编码（即UTF-16LE）
//        Charset ucs2Charset = StandardCharsets.UTF_16BE; // 使用UCS-2编码（即UTF-16LE）
        ByteBuffer utf8Bytes = utf8Charset.encode(original);
        CharBuffer ucs2Chars = ucs2Charset.decode(utf8Bytes);

        // 将UCS-2编码的字符重新编码为UTF-8编码
        ByteBuffer ucs2Bytes = ucs2Charset.encode(ucs2Chars);
        CharBuffer utf8Chars = utf8Charset.decode(ucs2Bytes);
        String result = utf8Chars.toString();

        System.out.println("Original string: " + original);
        System.out.println("Converted string: " + result);
    }

    private static void test2() {
        String original = "【JiaYing】Your verification code is 254573.Please complete the registration within 30 minutes.";

        // 将字符串从UTF-8编码转换为UCS-2编码
        Charset utf8Charset = StandardCharsets.UTF_8;
//        Charset ucs2Charset = StandardCharsets.UTF_16LE; // 使用UCS-2编码（即UTF-16LE）
        Charset ucs2Charset = StandardCharsets.UTF_16BE; // 使用UCS-2编码（即UTF-16LE）
        ByteBuffer utf8Bytes = utf8Charset.encode(original);
        CharBuffer ucs2Chars = ucs2Charset.decode(utf8Bytes);
        // 将CharBuffer转换为字节数组
//        ByteBuffer byteBuffer = ByteBuffer.encode(charBuffer);
//        byte[] bytes = byteBuffer.array();

//        ucs2Chars.
        // 将UCS-2编码的字符重新编码为UTF-8编码
        ByteBuffer ucs2Bytes = ucs2Charset.encode(ucs2Chars);
        CharBuffer utf8Chars = utf8Charset.decode(ucs2Bytes);
//        String result = utf8Chars.toString();

        System.out.println("Original string: " + original);
        String s = encodeUCS2(original);
        System.out.println("Converted string: " + s);
        String s2 = decodeUCS2(original);
        System.out.println("Converted string2: " + s2);
    }


    /**
     * UCS2解码
     *
     * @param src UCS2 源串
     * @return 解码后的UTF-16BE字符串
     */
    public static String decodeUCS2(String src) {
        byte[] bytes = new byte[src.length() / 2];

        for (int i = 0; i < src.length(); i += 2) {
            bytes[i / 2] = (byte) (Integer
                    .parseInt(src.substring(i, i + 2), 16));
        }
        String reValue = "";
        try {
            reValue = new String(bytes, "UTF-16BE");
        } catch (UnsupportedEncodingException e) {
        }
        return reValue;

    }

    /**
     * UCS2编码
     *
     * @param src UTF-16BE编码的源串
     * @return 编码后的UCS2串
     */
    public static String encodeUCS2(String src) {

        byte[] bytes = new byte[0];
        try {
            bytes = src.getBytes("UTF-16BE");
        } catch (UnsupportedEncodingException e) {
        }

        StringBuffer reValue = new StringBuffer();
        StringBuffer tem = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            tem.delete(0, tem.length());
            tem.append(Integer.toHexString(bytes[i] & 0xFF));
            if (tem.length() == 1) {
                tem.insert(0, '0');
            }
            reValue.append(tem);
        }
        return reValue.toString().toUpperCase();
    }
}